<template>
  <div class="fillcontain">
    <el-page-header @back="$router.go(-1)" content="查看总表"> </el-page-header>
    <!-- 搜索框和按钮 -->
    <div class="box">
      <el-form :inline="true" :model="search" class="demo-form-inline">
        <el-form-item label="物资编码识别结果统计表">
          <!-- <el-button type="success" @click="downloadTable2()"> 下载 </el-button> -->
        </el-form-item>
      </el-form>
    </div>

    <!-- 文件表格 -->
    <div class="table_container box">
      <el-table
        ref="table0"
        :data="tableData"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          property="fac"
          label="电厂"
          width="100"
        ></el-table-column>
        <el-table-column
          property="file_num"
          label="文件编码"
          width="100"
        ></el-table-column>
        <el-table-column
          property="spv"
          label="spv相关"
          width="100"
        ></el-table-column>
        <el-table-column
          property="file_name"
          label="文件名"
          width="200"
        ></el-table-column>
        <el-table-column
          property="file_size"
          label="文件大小"
          width="100"
        ></el-table-column>
        <el-table-column
          :formatter="codeNum"
          label="提取编码数"
          width="100"
        ></el-table-column>
        <el-table-column
          property="right_num"
          label="现有编码数"
          width="100"
        ></el-table-column>
        <el-table-column
          property="fault_num"
          label="失效编码数"
          width="100"
        ></el-table-column>
        <el-table-column
          property="fault_per"
          label="疑似占比"
          width="100"
        ></el-table-column>
        <!-- <el-table-column
          property="rec_num"
          label="推荐编码数"
          width="100"
        ></el-table-column> -->
        <el-table-column
          property="facAF_num"
          label="全部冻结（工厂级）"
          width="100"
        ></el-table-column>
        <el-table-column
          property="facFB_num"
          label="冻结采购（工厂级）"
          width="100"
        ></el-table-column>
        <el-table-column
          property="gpAF_num"
          label="全部冻结（集团级）"
          width="100"
        ></el-table-column>
        <el-table-column
          property="gpFB_num"
          label="冻结采购（集团级）"
          width="100"
        ></el-table-column>
      </el-table>
      <div class="Pagination" style="text-align: left; margin-top: 10px">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="20"
          layout="total, prev, pager, next"
          :total="count"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { getTotalResult } from "../api/getData";

export default {
  data() {
    return {
      search: {
        code: "",
      },
      tableData: [],
      currentRow: 20,
      offset: 0,
      limit: 10,
      count: 0,
      currentPage: 1,
    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 获取表格数据
    async getData() {
      try {
        const data = await getTotalResult(this.currentPage);

        if (data.success) {
          let list = data.list;
          // 计算疑似比率并插入
          list = list.map((item) => {
            let totalNum = item.fault_num + item.right_num;
            let FaultRate =
              ((item.fault_num / totalNum) * 100).toFixed(2) + "%";
            item.fault_per = totalNum ? FaultRate : 0;
            return item;
          });
          this.tableData = list;
          this.count = data.count;
        } else {
          throw new Error("获取数据失败");
        }
      } catch (err) {
        console.log("获取数据失败", err);
      }
    },
    // 每页显示的数据数改变
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    // 页面数改变重新获取数据
    handleCurrentChange(val) {
      this.currentPage = val;
      this.offset = (val - 1) * this.limit;
      this.getData();
    },
    // 计算提取编码数
    codeNum(row, column) {
      return row.right_num + row.fault_num;
    },
  },
};
</script>

<style lang="less">
// @import url("../style/mixin.less");
@import url("../assets/css/main.less");
.box {
  padding: 20px;
  margin-bottom: 20px;
  border: 1px solid #eee;
  border-radius: 4px;
  .el-form-item {
    margin: 20px 20px 0 0;
  }
}
</style>
